<?php
/**
 *
 * Created by PhpStorm.
 * User: Egor <http://gitee.com/seosite>
 * Date: 2018/8/9
 * Time: 上午11:15
 * Updatetime:
 * updater：
 */
namespace App\Controller;


class FormController extends BaseController
{

    public function __check($start_time,$end_time,$car_no)
    {

        if ($start_time>$end_time) return showErrorMsg('开始时间不能比结束时间大',100002);

        $uid = $this->userInfo->id;

        $carInfo = M('car')->where(['car_no'=>$car_no,'uid'=>$uid])->find();

        if (empty($carInfo)) return showErrorMsg(L("_DATA_NOT_EXIT_"),100002);

        return true;
    }

    /**
     * @desc 里程报表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function mileage($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('car_no,type');

        unset($check);


        $type = $param['type'];

        if ($type==1){//日

            $start_time = strtotime(date('Y-m-d',time()));
            $end_time   = strtotime(date('Y-m-d 23:59:59',time()));

        }elseif ($type==2){//周

            $start_time=mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y'));
            $end_time=mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y'));

        }else{//月

            $start_time = strtotime(date('Y-m-d 0:0:0', mktime(0,0,0,date('n'),1,date('Y'))));
            $end_time =  strtotime(date('Y-m-d 23:59:59', mktime(0,0,0,date('n'),date('t'),date('Y'))));

        }
        $car_no = $param['car_no'];

        if ($this->__check($start_time,$end_time,$car_no) !== true) return $this->__check($start_time,$end_time,$car_no);

        $map['x'] = array('between',array($start_time,$end_time));

        $map['car_id'] = M('car')->where(['car_no'=>$car_no])->getField('id');

        $model = M('car_attr');

        if ($type == 1){//日
            $ret= $model
                ->field("from_unixtime(create_time,'%Y/%m/%d %H:00') x,sum(today_mileage) y")
                ->where($map)
                ->group("x")
                ->select();

        }else{
            $ret= $model
                ->field("from_unixtime(create_time,'%Y/%m/%d') x,sum(today_mileage) y")
                ->where($map)
                ->group("x")
                ->select();
        }

        return showList($ret,new \stdClass());
    }


    /**
     * @desc 定位报表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function location($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('car_no,type');

        unset($check);


        $type = $param['type'];

        if ($type==1){//日

            $start_time = strtotime(date('Y-m-d',time()));
            $end_time   = strtotime(date('Y-m-d 23:59:59',time()));

        }elseif ($type==2){//周

            $start_time=mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y'));
            $end_time=mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y'));

        }else{//月

            $start_time = strtotime(date('Y-m-d 0:0:0', mktime(0,0,0,date('n'),1,date('Y'))));
            $end_time =  strtotime(date('Y-m-d 23:59:59', mktime(0,0,0,date('n'),date('t'),date('Y'))));

        }
        $car_no = $param['car_no'];

        if ($this->__check($start_time,$end_time,$car_no) !== true) return $this->__check($start_time,$end_time,$car_no);

        $map['x'] = array('between',array($start_time,$end_time));

        $map['car_id'] = M('car')->where(['car_no'=>$car_no])->getField('id');

        $model = M('car_attr');

        if ($type == 1){//日
            $ret= $model
                ->field("from_unixtime(create_time,'%Y/%m/%d %H:00') x,sum(today_mileage) y")
                ->where($map)
                ->group("x")
                ->select();

        }else{
            $ret= $model
                ->field("from_unixtime(create_time,'%Y/%m/%d') x,sum(today_mileage) y")
                ->where($map)
                ->group("x")
                ->select();
        }

        return showList($ret,new \stdClass());


    }

    /**
     * @desc 温湿度报表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function temAndHum($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('car_no,type');

        unset($check);


        $type = $param['type'];

        if ($type==1){//日

            $start_time = strtotime(date('Y-m-d',time()));
            $end_time   = strtotime(date('Y-m-d 23:59:59',time()));

        }elseif ($type==2){//周

            $start_time=mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y'));
            $end_time=mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y'));

        }else{//月

            $start_time = strtotime(date('Y-m-d 0:0:0', mktime(0,0,0,date('n'),1,date('Y'))));
            $end_time =  strtotime(date('Y-m-d 23:59:59', mktime(0,0,0,date('n'),date('t'),date('Y'))));

        }
        $car_no = $param['car_no'];

        if ($this->__check($start_time,$end_time,$car_no) !== true) return $this->__check($start_time,$end_time,$car_no);

        $map['x'] = array('between',array($start_time,$end_time));

        $map['car_id'] = M('car')->where(['car_no'=>$car_no])->getField('id');

        $model = M('car_attr');

        if ($type == 1){//日
            $ret= $model
                ->field("from_unixtime(create_time,'%Y/%m/%d %H:00') x,avg(humidity) y1,avg(temperature_one_area) y2")
                ->where($map)
                ->group("x")
                ->select();

        }else{
            $ret= $model
                ->field("from_unixtime(create_time,'%Y/%m/%d') x,avg(humidity) y1,avg(temperature_one_area) y2")
                ->where($map)
                ->group("x")
                ->select();
        }

        return showList($ret,new \stdClass());

    }


    /**
     * @desc 冷机运行报表 以下有坑勿怪
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function coldCar($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('s_time,e_time,car_no');

        unset($check);

//        if ($type==1){//日
//
//            $start_time = strtotime(date('Y-m-d',time()));
//            $end_time   = strtotime(date('Y-m-d 23:59:59',time()));
//
//        }elseif ($type==2){//周
//
//            $start_time=mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y'));
//            $end_time=mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y'));
//
//        }else{//月
//
//            $start_time = strtotime(date('Y-m-d 0:0:0', mktime(0,0,0,date('n'),1,date('Y'))));
//            $end_time =  strtotime(date('Y-m-d 23:59:59', mktime(0,0,0,date('n'),date('t'),date('Y'))));
//
//        }

        $start_time = strtotime($param['s_time']);
        $end_time = strtotime($param['e_time']);

        $type = ($end_time-$start_time) < 86400 ?1:2;

        $car_no = $param['car_no'];

        if ($this->__check($start_time,$end_time,$car_no) !== true) return $this->__check($start_time,$end_time,$car_no);

        $map['x'] = array('between',array($start_time,$end_time));

        $map['car_id'] = M('car')->where(['car_no'=>$car_no])->getField('id');

        $model = M('car_attr');

        if ($type == 1){//日
            $ret= $model
                ->field("from_unixtime(create_time,'%Y/%m/%d %H:00') x,cold_switch y")
                ->where($map)
                ->group("x")
                ->select();

        }else{
            $ret= $model
                ->field("from_unixtime(create_time,'%Y/%m/%d') x,cold_switch y")
                ->where($map)
                ->group("x")
                ->select();
        }

        return showList($ret,new \stdClass());

    }


    /**
     * @desc 门禁报表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function door($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('s_time,e_time,car_no');

        unset($check);

//        if ($type==1){//日
//
//            $start_time = strtotime(date('Y-m-d',time()));
//            $end_time   = strtotime(date('Y-m-d 23:59:59',time()));
//
//        }elseif ($type==2){//周
//
//            $start_time=mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y'));
//            $end_time=mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y'));
//
//        }else{//月
//
//            $start_time = strtotime(date('Y-m-d 0:0:0', mktime(0,0,0,date('n'),1,date('Y'))));
//            $end_time =  strtotime(date('Y-m-d 23:59:59', mktime(0,0,0,date('n'),date('t'),date('Y'))));
//
//        }

        $start_time = strtotime($param['s_time']);
        $end_time = strtotime($param['e_time']);

        $type = ($end_time-$start_time) < 86400 ?1:2;

        $car_no = $param['car_no'];

        if ($this->__check($start_time,$end_time,$car_no) !== true) return $this->__check($start_time,$end_time,$car_no);

        $map['x'] = array('between',array($start_time,$end_time));

        $map['car_id'] = M('car')->where(['car_no'=>$car_no])->getField('id');

        $model = M('car_attr');

        if ($type == 1){//日
            $ret= $model
                ->field("from_unixtime(create_time,'%Y/%m/%d %H:00') x,door y")
                ->where($map)
                ->group("x")
                ->select();

        }else{
            $ret= $model
                ->field("from_unixtime(create_time,'%Y/%m/%d') x,door y")
                ->where($map)
                ->group("x")
                ->select();
        }

        return showList($ret,new \stdClass());

    }


    /**
     * @desc 围栏报表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function enclosure($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('s_time,e_time,car_no');

        unset($check);

//        if ($type==1){//日
//
//            $start_time = strtotime(date('Y-m-d',time()));
//            $end_time   = strtotime(date('Y-m-d 23:59:59',time()));
//
//        }elseif ($type==2){//周
//
//            $start_time=mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y'));
//            $end_time=mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y'));
//
//        }else{//月
//
//            $start_time = strtotime(date('Y-m-d 0:0:0', mktime(0,0,0,date('n'),1,date('Y'))));
//            $end_time =  strtotime(date('Y-m-d 23:59:59', mktime(0,0,0,date('n'),date('t'),date('Y'))));
//
//        }

        $start_time = strtotime($param['s_time']);
        $end_time = strtotime($param['e_time']);

        $type = ($end_time-$start_time) < 86400 ?1:2;

        $car_no = $param['car_no'];

        if ($this->__check($start_time,$end_time,$car_no) !== true) return $this->__check($start_time,$end_time,$car_no);

        $map['x'] = array('between',array($start_time,$end_time));

        $map['car_id'] = M('car')->where(['car_no'=>$car_no])->getField('id');

        $model = M('car_enclosure');

        if ($type == 1){//日
            $ret= $model
                ->field("from_unixtime(intime,'%Y/%m/%d %H:00') x,enclosure_id")
                ->where($map)
                ->group("x")
                ->select();

        }else{
            $ret= $model
                ->field("from_unixtime(intime,'%Y/%m/%d') x,enclosure_id")
                ->where($map)
                ->group("x")
                ->select();
        }

        if (!empty($ret)){
            foreach ($ret as $k=>$v){
                $enclosureInfo = M('enclosure')->where(['id'=>$v['enclosure_id']])->find();

                $ret[$k]['y'] = !empty($enclosureInfo)?$enclosureInfo['title']:'';
                $ret[$k]['enclosure_type'] = !empty($enclosureInfo)?$enclosureInfo['shape']:'';

            }
        }

        return showList($ret,new \stdClass());

    }


    /**
     * @desc 停车报表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function park($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('s_time,e_time,car_no');

        unset($check);

//        if ($type==1){//日
//
//            $start_time = strtotime(date('Y-m-d',time()));
//            $end_time   = strtotime(date('Y-m-d 23:59:59',time()));
//
//        }elseif ($type==2){//周
//
//            $start_time=mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y'));
//            $end_time=mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y'));
//
//        }else{//月
//
//            $start_time = strtotime(date('Y-m-d 0:0:0', mktime(0,0,0,date('n'),1,date('Y'))));
//            $end_time =  strtotime(date('Y-m-d 23:59:59', mktime(0,0,0,date('n'),date('t'),date('Y'))));
//
//        }

        $start_time = strtotime($param['s_time']);
        $end_time = strtotime($param['e_time']);

        $type = ($end_time-$start_time) < 86400 ?1:2;

        $car_no = $param['car_no'];

        if ($this->__check($start_time,$end_time,$car_no) !== true) return $this->__check($start_time,$end_time,$car_no);

        $map['x'] = array('between',array($start_time,$end_time));

        $map['car_id'] = M('car')->where(['car_no'=>$car_no])->getField('id');

        $model = M('car_attr');

        if ($type == 1){//日
            $ret= $model
                ->field("from_unixtime(create_time,'%Y/%m/%d %H:00') x,stop_time y,stop_address")
                ->where($map)
                ->group("x")
                ->select();

        }else{
            $ret= $model
                ->field("from_unixtime(create_time,'%Y/%m/%d') x,stop_time y")
                ->where($map)
                ->group("x")
                ->select();
        }

        return showList($ret,new \stdClass());

    }


    /**
     * @desc 油量报表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function oil($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('s_time,e_time,car_no');

        unset($check);

//        if ($type==1){//日
//
//            $start_time = strtotime(date('Y-m-d',time()));
//            $end_time   = strtotime(date('Y-m-d 23:59:59',time()));
//
//        }elseif ($type==2){//周
//
//            $start_time=mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y'));
//            $end_time=mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y'));
//
//        }else{//月
//
//            $start_time = strtotime(date('Y-m-d 0:0:0', mktime(0,0,0,date('n'),1,date('Y'))));
//            $end_time =  strtotime(date('Y-m-d 23:59:59', mktime(0,0,0,date('n'),date('t'),date('Y'))));
//
//        }

        $start_time = strtotime($param['s_time']);
        $end_time = strtotime($param['e_time']);

        $type = ($end_time-$start_time) < 86400 ?1:2;

        $car_no = $param['car_no'];

        if ($this->__check($start_time,$end_time,$car_no) !== true) return $this->__check($start_time,$end_time,$car_no);

        $map['x'] = array('between',array($start_time,$end_time));

        $map['car_id'] = M('car')->where(['car_no'=>$car_no])->getField('id');

        $model = M('car_attr');

        if ($type == 1){//日
            $ret= $model
                ->field("from_unixtime(create_time,'%Y/%m/%d %H:00') x,oil y")
                ->where($map)
                ->group("x")
                ->select();

        }else{
            $ret= $model
                ->field("from_unixtime(create_time,'%Y/%m/%d') x,oil y")
                ->where($map)
                ->group("x")
                ->select();
        }

        return showList($ret,new \stdClass());

    }


    /**
     * @desc 警告报表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function warning($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('s_time,e_time,car_no');

        unset($check);

//        if ($type==1){//日
//
//            $start_time = strtotime(date('Y-m-d',time()));
//            $end_time   = strtotime(date('Y-m-d 23:59:59',time()));
//
//        }elseif ($type==2){//周
//
//            $start_time=mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y'));
//            $end_time=mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y'));
//
//        }else{//月
//
//            $start_time = strtotime(date('Y-m-d 0:0:0', mktime(0,0,0,date('n'),1,date('Y'))));
//            $end_time =  strtotime(date('Y-m-d 23:59:59', mktime(0,0,0,date('n'),date('t'),date('Y'))));
//
//        }

        $start_time = strtotime($param['s_time']);
        $end_time = strtotime($param['e_time']);

        $type = ($end_time-$start_time) < 86400 ?1:2;

        $car_no = $param['car_no'];

        if ($this->__check($start_time,$end_time,$car_no) !== true) return $this->__check($start_time,$end_time,$car_no);

        $map['x'] = array('between',array($start_time,$end_time));

        $map['car_id'] = M('car')->where(['car_no'=>$car_no])->getField('id');

        $map['uid'] = $this->userInfo->id;

        $model = M('message');

        if ($type == 1){//日
            $ret= $model
                ->field("from_unixtime(create_time,'%Y/%m/%d %H:00') x,warning_type y")
                ->where($map)
                ->group("x")
                ->select();

        }else{
            $ret= $model
                ->field("from_unixtime(create_time,'%Y/%m/%d') x,warning_type y")
                ->where($map)
                ->group("x")
                ->select();
        }

        return showList($ret,new \stdClass());

    }


    /**
     * @desc 汇总报表
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function summary($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('s_time,e_time,car_no');

        unset($check);

//        if ($type==1){//日
//
//            $start_time = strtotime(date('Y-m-d',time()));
//            $end_time   = strtotime(date('Y-m-d 23:59:59',time()));
//
//        }elseif ($type==2){//周
//
//            $start_time=mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y'));
//            $end_time=mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y'));
//
//        }else{//月
//
//            $start_time = strtotime(date('Y-m-d 0:0:0', mktime(0,0,0,date('n'),1,date('Y'))));
//            $end_time =  strtotime(date('Y-m-d 23:59:59', mktime(0,0,0,date('n'),date('t'),date('Y'))));
//
//        }

        $start_time = strtotime($param['s_time']);
        $end_time = strtotime($param['e_time']);


        $type = ($end_time-$start_time) < 86400 ?1:2;


        $car_no = $param['car_no'];

        if ($this->__check($start_time,$end_time,$car_no) !== true) return $this->__check($start_time,$end_time,$car_no);

        $map['x'] = array('between',array($start_time,$end_time));

        $map['car_id'] = M('car')->where(['car_no'=>$car_no])->getField('id');

        $model = M('car_attr');

        if ($type == 1){//日
            $ret= $model
                ->field("from_unixtime(create_time,'%Y/%m/%d %H:00') x, humidity y1,temperature_one_area y2,car_status y3")
                ->where($map)
                ->group("x")
                ->select();

        }else{
            $ret= $model
                ->field("from_unixtime(create_time,'%Y/%m/%d') x,humidity y1,temperature_one_area y2,car_status y3")
                ->where($map)
                ->group("x")
                ->select();
        }

        return showList($ret,new \stdClass());

    }

    /**
     * @desc
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function carList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $map['uid'] = $this->userInfo->id;

        $list = M('car')->field('id,car_no')->where($map)->select();

        return showList($list,new \stdClass());
    }

}